//
//  GADTableViewCell.m
//  Wifi
//
//  Created by srulos on 2017/6/28.
//  Copyright © 2017年 srulos. All rights reserved.
//

#import "GADTableViewCell.h"
#import "LoginHeader.h"
#import <GoogleMobileAds/GoogleMobileAds.h>

@interface GADTableViewCell()<GADBannerViewDelegate>
{
    GADBannerView * m_gadBannerView;
    UIActivityIndicatorView * m_activityIndicator;
}
@end

@implementation GADTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (void)setup:(UIViewController *)rootViewController
{
    CGFloat height = 0;
    if ([[UIDevice currentDevice].model rangeOfString:@"iPad"].length > 0) {
        height = __kwidth * 0.8;
    }else{
        height =  __kwidth ;
    }
    m_gadBannerView = [[GADBannerView alloc] initWithFrame:CGRectMake(0, 0,__kwidth ,height)];
    m_gadBannerView.adUnitID = @"ca-app-pub-4634576436183649/6594093789";//@"ca-app-pub-4634576436183649/1988689899";
    m_gadBannerView.rootViewController = rootViewController;
    m_gadBannerView.delegate = self;
    [m_gadBannerView loadRequest:[GADRequest request]];
    m_gadBannerView.backgroundColor = [UIColor whiteColor];
    [self setFrame:m_gadBannerView.frame];
    
    m_activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    m_activityIndicator.center = self.center;
    [self.contentView addSubview:m_activityIndicator];
    [m_activityIndicator startAnimating];
}

/// Tells the delegate that an ad request successfully received an ad. The delegate may want to add
/// the banner view to the view hierarchy if it hasn't been added yet.
- (void)adViewDidReceiveAd:(GADBannerView *)bannerView
{
    if (m_activityIndicator != nil && m_activityIndicator.superview != nil) {
        [m_activityIndicator removeFromSuperview];
    }
    
    [self.contentView addSubview:m_gadBannerView];
}
/// Tells the delegate that an ad request failed. The failure is normally due to network
/// connectivity or ad availablility (i.e., no fill).
- (void)adView:(GADBannerView *)bannerView didFailToReceiveAdWithError:(GADRequestError *)error
{
    
}

@end
